package _binary_search;

import java.util.Arrays;

/**
 * 611. 有效三角形的个数
 */
public class No611 {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int answer = 0;
        for (int i = nums.length - 1; i > 1; i--) {
            int left = 0, right = i - 1;
            while (left < right) {
                if (nums[left] + nums[right] <= nums[i]) left++;
                else answer += right-- - left;
            }
        }
        return answer;
    }
}
